package product

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/product"
	productReq "github.com/flipped-aurora/gin-vue-admin/server/model/product/request"
	"gorm.io/gorm"
)

type BsProductInfoService struct {
}

// CreateBsProductInfo 创建二手商品记录
// Author [piexlmax](https://github.com/piexlmax)
func (bsProductInfoService *BsProductInfoService) CreateBsProductInfo(bsProductInfo *product.BsProductInfo) (err error) {
	err = global.GVA_DB.Create(bsProductInfo).Error
	return err
}

// DeleteBsProductInfo 删除二手商品记录
// Author [piexlmax](https://github.com/piexlmax)
func (bsProductInfoService *BsProductInfoService) DeleteBsProductInfo(id string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&product.BsProductInfo{}).Where("id = ?", id).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&product.BsProductInfo{}, "id = ?", id).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteBsProductInfoByIds 批量删除二手商品记录
// Author [piexlmax](https://github.com/piexlmax)
func (bsProductInfoService *BsProductInfoService) DeleteBsProductInfoByIds(ids []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&product.BsProductInfo{}).Where("id in ?", ids).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", ids).Delete(&product.BsProductInfo{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateBsProductInfo 更新二手商品记录
// Author [piexlmax](https://github.com/piexlmax)
func (bsProductInfoService *BsProductInfoService) UpdateBsProductInfo(bsProductInfo product.BsProductInfo) (err error) {
	err = global.GVA_DB.Updates(&bsProductInfo).Error
	return err
}

// GetBsProductInfo 根据id获取二手商品记录
// Author [piexlmax](https://github.com/piexlmax)
func (bsProductInfoService *BsProductInfoService) GetBsProductInfo(id string) (bsProductInfo product.BsProductInfo, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&bsProductInfo).Error
	return
}

// GetBsProductInfoInfoList 分页获取二手商品记录
// Author [piexlmax](https://github.com/piexlmax)
func (bsProductInfoService *BsProductInfoService) GetBsProductInfoInfoList(info productReq.BsProductInfoSearch) (list []product.BsProductInfo, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&product.BsProductInfo{})
	var bsProductInfos []product.BsProductInfo
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Category != "" {
		db = db.Where("category = ?", info.Category)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["price"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&bsProductInfos).Error
	return bsProductInfos, total, err
}
